//
//  MyWebView.m
//  ZhuiKe55like
//
//  Created by junseek on 15-4-13.
//  Copyright (c) 2015年 五五来客 李江. All rights reserved.
//

#import "MyWebView.h"
#import "EGORefreshTableHeaderView.h"
#import "MessageInterceptor.h"
//#import "XHImageUrlViewer.h"
#import "WebImagesViewController.h"

@interface MyWebView ()<EGORefreshTableHeaderDelegate,UIWebViewDelegate>{
    //
    EGORefreshTableHeaderView *refreshHeaderView;
    //刷新标识，是否正在刷新过程中
    BOOL reloading;
    NSString *strTempUrl;
    
//    XHImageUrlViewer *xhView;
    NSMutableArray *arrayImages;
    UIProgressView *progressIndicator;
    NSTimer *timeProgress;
}

@property (nonatomic, strong) NSString *strUrl;
@property (nonatomic, strong) MessageInterceptor *delegateInterceptor;
@end


@implementation MyWebView
@synthesize delegateInterceptor=_delegateInterceptor;



-(instancetype)init{
    return [self initWithFrame:[[UIScreen mainScreen] bounds]];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initComponents];
    }
    
    return self;
}

- (void)setDelegate:(id<UIWebViewDelegate>)delegate
{
    if(_delegateInterceptor) {
        super.delegate = nil;
        _delegateInterceptor.receiver = delegate;
        super.delegate = (id)_delegateInterceptor;
    } else {
        super.delegate = delegate;
    }
}

- (void)initComponents
{
    arrayImages=[NSMutableArray new];
    self.backgroundColor=[UIColor whiteColor];
    _delegateInterceptor = [[MessageInterceptor alloc] init];
    _delegateInterceptor.middleMan = self;
    _delegateInterceptor.receiver = self.delegate;
    //初始化refreshView，添加到webview 的 scrollView子视图中
    if (refreshHeaderView == nil) {
        refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0-self.scrollView.bounds.size.height, self.scrollView.frame.size.width, self.scrollView.bounds.size.height)];
        refreshHeaderView.delegate = self;
        self.scrollView.delegate=self;
        [self.scrollView addSubview:refreshHeaderView];
    }
    if (!progressIndicator) {
        progressIndicator = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, W(self), 1)];
        [progressIndicator setProgressViewStyle:UIProgressViewStyleBar];
        [progressIndicator setProgressTintColor:rgbpublicColor];
        [progressIndicator setTrackTintColor:[UIColor clearColor]];
        progressIndicator.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleWidth;
        [self addSubview:progressIndicator];
        progressIndicator.hidden=YES;
//        timeProgress=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(function:) userInfo:nil repeats:YES];

    }
   
    
    
    [refreshHeaderView refreshLastUpdatedDate];
}

-(void)loadMyWeb:(NSString *)url{
    strTempUrl=url;
    self.strUrl=url;
    [self refreshWeb];
}
-(void)refreshWeb{
    DLog("refreshWeb:%@",_strUrl)
    //清除UIWebView的缓存
   [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *encodedString=[self.strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:encodedString]];
        [self loadRequest:request];
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
    });

}
#pragma mark controller
- (BaseViewController *)viewBaseController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[BaseViewController class]]) {
            return (BaseViewController *)nextResponder;
        }
    }
    return nil;
}
#pragma mark -
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    self.strUrl=[request.URL absoluteString];
    BOOL bool_return=YES;
    //将url转换为string
    NSString *requestString = [[request URL] absoluteString];
    //    NSLog(@"requestString is %@",requestString);
    
    //hasPrefix 判断创建的字符串内容是否以pic:字符开始
    if ([requestString hasPrefix:@"myweb:imageClick:"]) {
        NSString *imageUrl = [requestString substringFromIndex:@"myweb:imageClick:".length];
        //        NSLog(@"image url------%@", imageUrl);
        
//        if (!xhView) {
//            xhView=[[XHImageUrlViewer alloc] init];
//        }
        NSInteger index_img=0;
        NSMutableArray *array=[[NSMutableArray alloc] init];
        for (int i=0 ; i<[arrayImages count];i++) {
            NSString *strImg =[arrayImages objectAtIndex:i];
            if ([strImg isEqualToString:imageUrl]) {
                index_img=i;
            }
            NSMutableDictionary *dt=[[NSMutableDictionary alloc] init];
            [dt setValue:strImg forKey:@"url"];
            [array addObject:dt];
        }
        [[self viewBaseController] pushController:[WebImagesViewController class] withInfo:@"myWebView" withTitle:@"查看" withOther:@{
                                                                                                                                @"lookImages":array,
                                                                                                                                @"lookSelectIndex":[NSString stringWithFormat:@"%ld",(long)index_img]}];
        
//        [xhView showWithImageDatas:array selectedIndex:index_img];
        return NO;
    }
    if (_delegateInterceptor !=nil && [_delegateInterceptor.receiver respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
        bool_return=[_delegateInterceptor.receiver webView:self shouldStartLoadWithRequest:request navigationType:navigationType];
    }
    return bool_return;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [arrayImages removeAllObjects];
    reloading = YES;
    progressIndicator.hidden=NO;
    progressIndicator.progress=0.0;
    [progressIndicator setProgress:0.9 animated:YES];;
    
    if ([_delegateInterceptor.receiver respondsToSelector:@selector(webViewDidStartLoad:)]) {
        [_delegateInterceptor.receiver webViewDidStartLoad:self];
    }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    reloading = NO;
    progressIndicator.progress=1.0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        progressIndicator.hidden=YES;
        progressIndicator.progress=0.0;
    });
   
//    //调整字号
//    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '95%'";
//    [webView stringByEvaluatingJavaScriptFromString:str];
    
    //js方法遍历图片添加点击事件 返回图片点击个数
    static  NSString * const jsGetImages =
    @"function getImages(){\
    var image_url=\"\";\
    var index_click=0;\
    var objs = document.getElementsByTagName(\"img\");\
    for(var i=0;i<objs.length;i++){\
        if(objs[i].getAttribute(\"data-src\")){\
            if(image_url){\
                image_url=image_url+\"__zkwap__\"+objs[i].getAttribute(\"data-src\");\
            }else{\
                image_url=objs[i].getAttribute(\"data-src\");\
            };\
            index_click++;\
            objs[i].onclick=function(){\
            document.location=\"myweb:imageClick:\"+this.getAttribute(\"data-src\");;\
        };\
    };\
    };\
    return image_url;\
    };";
   
    
    [webView stringByEvaluatingJavaScriptFromString:jsGetImages];//注入js方法
   
    //注入自定义的js方法后别忘了调用 否则不会生效（不调用也一样生效了，，，不明白）
    NSString *resurlt = [webView stringByEvaluatingJavaScriptFromString:@"getImages()"];
    //调用js方法
    DLog(@"---调用js方法--%@ jsMehtods_result = %@",self.class,resurlt);

  
    
    [arrayImages removeAllObjects];
    if ([resurlt notEmptyOrNull]) {
        [arrayImages addObjectsFromArray:[resurlt componentsSeparatedByString:@"__zkwap__"]];
    }
    
    
    
    
    self.strUrl = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    if (refreshHeaderView) {
        [refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.scrollView];
    }
    
    if (_delegateInterceptor !=nil && [_delegateInterceptor.receiver respondsToSelector:@selector(webViewDidFinishLoad:)]) {
        [_delegateInterceptor.receiver webViewDidFinishLoad:self];
    }
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    reloading = NO;
    progressIndicator.hidden=YES;
    progressIndicator.progress=0.0;
    if (refreshHeaderView) {
        [refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.scrollView];
    }
    
    if (_delegateInterceptor !=nil && [_delegateInterceptor.receiver respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
        [_delegateInterceptor.receiver webView:self didFailLoadWithError:error];
    }
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (kVersion7) {
         [super scrollViewDidScroll:scrollView];
    }
    if (refreshHeaderView) {
        [refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (kVersion7) {
        [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
    if (refreshHeaderView) {
        [refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    }
    

}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    [self performSelector:@selector(refreshWeb) withObject:nil afterDelay:.1];
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return reloading; // should return if data source model is reloading
    
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
}

-(void)dealloc{
    super.delegate = nil;
    _delegateInterceptor=nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
